package com.kabuda.zhugehot200;

/**
 * @author kabuda
 * @date 2025-03-16 15:36
 * @description
 */
public class 子数组最大平均值 {
    public static void main(String[] args) {
        System.out.println(findMaxAverage(new int[]{1, 4, 8, 9, 12, 26}, 4));
    }


    /**
     * @param nums
     * @param k
     * @author kabuda
     * @date 2025/3/16 15:43
     * @description 滑动窗口 :特殊的双指针(指针同方向同速度运行)
     */
    public static double findMaxAverage(int[] nums, int k) {
        int sum = 0;
        //初始化第一个窗口的总和
        for (int i = 0; i < k; i++) {
            sum += nums[i];
        }
        int max = sum;

        //不断的整体移动窗口
        for (int i = k; i < nums.length; i++) {
            // 总和减去头,加上尾
            sum = sum - nums[i - k] + nums[i];
            max = Math.max(max, sum);
        }

        return (double) max / k;
    }
}
